#include "HufArc.h"
int writeToArc ( FILE * arc, FILE * fileToRead, fileHead * fileHd)
{
	unsigned int CheckSum = 0;
	unsigned long long int count = 0;
	char * buf = (char*) calloc (SIZE_BUF,sizeof(char));
	int rc = 0;
	unsigned int sizeFileSz = 0;
	if (!buf)
			cl_exit (WRITE_TO_ARC,MEMORY_ERR,arc,fileToRead,fileHd);
	intWrite (arc,(unsigned long long int) fileHd->sizeName,NAME_LEN,&CheckSum);
	strWrite (arc,fileHd->fileName,fileHd->sizeName,&CheckSum);
	intWrite (arc, fileHd -> mtime, SIZE_T_8, &CheckSum);
	intWrite (arc,(unsigned long long int) fileHd->huff_ubits, SIZE_T_1,&CheckSum);
	if (fileHd->huff_ubits & LEN_8_B ) 
	{
		intWrite (arc, fileHd->fileSize, SIZE_T_8, &CheckSum);
		sizeFileSz = SIZE_T_8;
	} else {
		intWrite (arc, fileHd->fileSize, 4, &CheckSum);
		sizeFileSz = SIZE_T_4;
	}
	/*if (!(fileHd->huff_ubits & NO_HUFF))
		{
			//fileToRead = arcFile (fileToRead,&Huff_file_len);
			intWrite (arc, (unsigned long long int) fileHd->huff_block, 2,&CheckSum);
			intWrite (arc,fileHd->huff_file_len,sizeFileSz,&CheckSum);
		} else {*/
		for (;;)
		{
			if ((rc = fread (buf,sizeof(char),SIZE_BUF,fileToRead)) == SIZE_BUF)
			{
				count += rc;
				strWrite (arc,buf,SIZE_BUF,&CheckSum);
			} else {
				if (rc!= 0)	strWrite (arc,buf,rc,&CheckSum);
				count += rc;
				break;
			}
		}
		/*if (!(fileHd->huff_ubits & NO_HUFF))
			{
				if (count != fileHd->huff_file_len)
				{
					return 1;
				}
			} else {
				if (count != fileHd->fileSize)
				{
					return 1;
				}
			}
		}*/
	//printf ("old %u", CheckSum);
	intWrite (arc,CheckSum,4,&CheckSum);
	return 0;
}
